package com.nexgo.payment.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nexgo.payment.R;

/**
 * 自定义组合控件
 * 一行显示，左中右对齐
 * @author 谭忠扬-YuriTam
 * @time 2017年8月29日
 */
public class ItemLinearAlignLayout extends LinearLayout {
    private static final String TAG = ItemLinearAlignLayout.class.getSimpleName();

    //左边文字
    private TextView tv_left_text;
    //中间文字
    private TextView tv_center_text;
    //右边文字
    private TextView tv_right_text;
    //字体风格
    private int font_style = 0;

    public ItemLinearAlignLayout(Context context) {
        this(context,null);
    }

    public ItemLinearAlignLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ItemLinearAlignLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
        //初始化自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.ItemLinearAlignLayout);
        if (typedArray != null){
            //左边文字
            String leftText = typedArray.getString(R.styleable.ItemLinearAlignLayout_tv_left_text);
            if (!TextUtils.isEmpty(leftText)){
                tv_left_text.setText(leftText);
            }
            //中间文字
            String centerText = typedArray.getString(R.styleable.ItemLinearAlignLayout_tv_center_text);
            if (!TextUtils.isEmpty(centerText)){
                tv_center_text.setText(centerText);
            }
            //右边文字
            String rightText = typedArray.getString(R.styleable.ItemLinearAlignLayout_tv_right_text);
            if (!TextUtils.isEmpty(rightText)){
                tv_right_text.setText(rightText);
            }
            //字体风格
            int fontStyle = typedArray.getInt(R.styleable.ItemLinearAlignLayout_tv_font_style,font_style);
            if (fontStyle != 0){
                tv_left_text.getPaint().setFakeBoldText(true);
                tv_center_text.getPaint().setFakeBoldText(true);
                tv_right_text.getPaint().setFakeBoldText(true);
            }
            typedArray.recycle();
        }
    }

    private void initView(Context mContext){
        LayoutInflater.from(mContext).inflate(R.layout.item_line_left_center_right_layout,this,true);
        tv_left_text = (TextView) findViewById(R.id.tv_left);
        tv_center_text = (TextView) findViewById(R.id.tv_center);
        tv_right_text = (TextView) findViewById(R.id.tv_right);
    }

    public void setLeftText(String leftText) {
        if (!TextUtils.isEmpty(leftText)){
            tv_left_text.setText(leftText);
        }
    }

    public void setLeftText(int leftResId) {
        if (leftResId != 0){
            tv_left_text.setText(leftResId);
        }
    }

    public void setCenterText(String centerText) {
        if (!TextUtils.isEmpty(centerText)){
            tv_center_text.setText(centerText);
        }
    }

    public void setCenterText(int leftResId) {
        if (leftResId != 0){
            tv_center_text.setText(leftResId);
        }
    }

    public void setRightText(String rightText){
        if (!TextUtils.isEmpty(rightText)){
            tv_right_text.setText(rightText);
        }
    }

    public void setRightText(int rightResId){
        if (rightResId != 0){
            tv_right_text.setText(rightResId);
        }
    }

    public void setFontStyle(int fontStyle){
        if (fontStyle != 0){
            tv_left_text.getPaint().setFakeBoldText(true);
            tv_center_text.getPaint().setFakeBoldText(true);
            tv_right_text.getPaint().setFakeBoldText(true);
        }
    }
}
